package org.bjtu.hadooptest.hadoop;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;

import java.io.*;

public class RemoveOrMake {
    /**
     * 判断路径是否存在
     */
    public static boolean test(Configuration conf, String path) {
        try (FileSystem fs = FileSystem.get(conf)) {
            return fs.exists(new Path(path));
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 创建目录
     */
    public static boolean mkdir(Configuration conf, String remoteDir) {
        try (FileSystem fs = FileSystem.get(conf)) {
            Path dirPath = new Path(remoteDir);
            return fs.mkdirs(dirPath);
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 创建文件
     */
    public static void touchz(Configuration conf, String remoteFilePath) {
        Path remotePath = new Path(remoteFilePath);
        try (FileSystem fs = FileSystem.get(conf)) {
            FSDataOutputStream outputStream = fs.create(remotePath);
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 删除文件
     */
    public static boolean rm(Configuration conf, String remoteFilePath) {
        Path remotePath = new Path(remoteFilePath);
        try (FileSystem fs = FileSystem.get(conf)) {
            return fs.delete(remotePath, false);
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 主函数
     */
    public static void main(String[] args) {
        Configuration conf = new Configuration();
        conf.set("fs.defaultFS", "hdfs://172.31.42.153:9000");
        String remoteFilePath = "/user/root/text.txt"; // HDFS路径
        String remoteDir = "/user/root"; // HDFS路径对应的目录

        try {
            /* 判断路径是否存在，存在则删除，否则进行创建 */
            if (RemoveOrMake.test(conf, remoteFilePath)) {
                RemoveOrMake.rm(conf, remoteFilePath); // 删除
                System.out.println("删除文件: " + remoteFilePath);
            } else {
                if (!RemoveOrMake.test(conf, remoteDir)) { // 若目录不存在，则进行创建
                    RemoveOrMake.mkdir(conf, remoteDir);
                    System.out.println("创建文件夹: " + remoteDir);
                }
                RemoveOrMake.touchz(conf, remoteFilePath);
                System.out.println("创建文件: " + remoteFilePath);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

//    作者：Tiny_16
//    链接：https://www.jianshu.com/p/0663d74b79b5
//    來源：简书
//    著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。
}
